package org.lq.his.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.lq.his.pojo.Dictionary;
import org.lq.his.service.DictionaryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {
	
	@Resource
	private DictionaryService dictionaryService;

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	@RequestMapping("/index")
	public String index() throws Exception{
		
		return "dictionary/dictionaryIndex";
	}
	@RequestMapping("/selectValueByKey")
	@ResponseBody
	public List<Dictionary> selectValueByKey(String key)throws Exception{
		

		return dictionaryService.getDictionaryValueByKey(key);
		
	}
	
	@RequestMapping("/dParent")
	public String dictionaryParent() throws Exception{
		
		return "dictionary/dictionaryParent";
	}
	
	@RequestMapping("/dSubGrade")
	public String dictionarySubGrade() throws Exception{
		
		return "dictionary/dictionarySubGrade"; 
	}
	
	@RequestMapping("/all")
	@ResponseBody
	public List<Dictionary> dicAll() throws Exception{
		
		return dictionaryService.getAll();
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> getListByCondition(Integer page, Integer rows,@RequestParam(defaultValue="id") String sort,@RequestParam(defaultValue="asc") String order,Dictionary condition) throws Exception{
		Map<String, Object> map = new HashMap<>();
		
		int start = (page - 1) * rows;
		List<Dictionary> list = dictionaryService.getListByCondition(start, rows, condition, sort, order);
		int total = dictionaryService.getCountByCondition(condition);
		
		map.put("rows", list);
		map.put("total", total);	
		return map;
	}
	
	@RequestMapping("/parent")
	@ResponseBody
	public List<Dictionary> getParent() throws Exception{
		
		return dictionaryService.getParent();
	}
	
	@RequestMapping("/parentID")
	@ResponseBody
	public Dictionary getParentId(Integer parentId) throws Exception{
		
		return dictionaryService.getparentId(parentId);
	}
	
	@RequestMapping("/parentAdd")
	@ResponseBody
	public Map<String, Object> dictionaryParentAdd(Dictionary parent){
		Map<String, Object> map = new HashMap<>();
		dictionaryService.getAddParent(parent);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/subGradeAdd")
	@ResponseBody
	public Map<String, Object> dictionarySubGradeAdd(Dictionary subgrade){
		Map<String, Object> map = new HashMap<>();
		dictionaryService.add(subgrade);
		map.put("result", true);
		return map;
		
	}
	
	@RequestMapping("/selectById")
	@ResponseBody
	public Dictionary getById(Integer id) throws Exception{
		System.out.println(dictionaryService.getById(id));
		return dictionaryService.getById(id);
	}
	
	@RequestMapping("/dictionaryEdit")
	@ResponseBody
	public Map<String, Object> update(Dictionary dictionary){
		Map<String, Object> map = new HashMap<>();
		dictionaryService.update(dictionary);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/parentEdit")
	@ResponseBody
	public Map<String, Object> getEditParent(Dictionary dictionary){
		Map<String, Object> map = new HashMap<>();
		dictionaryService.getEditParent(dictionary);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/stateEdit")
	@ResponseBody
	public Map<String, Object> getEditState(Integer[] ids){
		Map<String, Object> map = new HashMap<>();
		dictionaryService.getEditState(ids);
		map.put("result", true);
		return map;
	}
}
